package com.atstudying.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Mr.zhneg
 * @create 2020-10-14-15:29
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set=new HashSet<>();
    //初始化方法
    @Override
    public void initialize(ListValue constraintAnnotation) {

        //获取详细信息vals={0,1}0,1
        int [] vals=constraintAnnotation.vals();
        for (int val :vals){
            set.add(val);
        }
    }

    //判断是否校验成功
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
       //如果我们获取值为3=value，传进来判断不会等于0,1就是返回false
        return set.contains(value);
    }
}
